//
//  Geocoder.m
//  跳转第三方地图
//
//  Created by apple on 2017/8/18.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "Geocoder.h"

@implementation Geocoder


+ (void)createDiLiCode:(NSString *)address success:(void (^)(NSMutableArray *))success failed:(void (^)(NSError *))failed{

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    //    NSString *address = @"安徽省六安市裕安区皖西学院";
    
    NSMutableArray *addressMutArr = [NSMutableArray arrayWithCapacity:0];
    
    // 容错
    if([address length] == 0)
        return;
    
    [geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        // CLPlacemark : 地标
        // location : 位置对象
        // addressDictionary : 地址字典
        // name : 地址详情
        // locality : 城市
        
        if(error == nil)
        {
            CLPlacemark *pl = [placemarks firstObject];
            
            float laiFloat = pl.location.coordinate.latitude;
            float lonFloat = pl.location.coordinate.longitude;
            
            NSString *laiStr = @(pl.location.coordinate.latitude).stringValue;
            NSString *lonStr = @(pl.location.coordinate.longitude).stringValue;
            
            //            NSLog(@"_lai = %f",_laiFloat);
            //            NSLog(@"_lon = %f",_lonFloat);
            
            NSLog(@"_laistr = %@",laiStr);
            NSLog(@"_lonstr = %@",lonStr);
            
            [addressMutArr addObject:laiStr];
            [addressMutArr addObject:lonStr];
            
            success(addressMutArr);
            
        }else
        {
            NSLog(@"错误");
        }
    }];
    

}

@end
